import _ from 'lodash';

const computeFcOptions = (tableColumns: Array<any>) => {
  const result = [];
  tableColumns.forEach((column, index) => {
    if (column.type !== 'seq') {
      // 删除无用(第一列)序号
      delete column.slots; // 删除无用属性
      result.push(column);
    }
  });
  return result;
};

const getFcOptions = (option?: any) => {
  return _.merge(
    {
      //实例对象
      fApi: {},
      //表单生成规则
      rule: [],
      //组件参数配置
      option: {
        form: {
          inline: true,
          layout: 'inline', // 这里用的inline模式
        },
        submitBtn: {
          innerText: '查询',
        },
        resetBtn: {
          show: true,
        },
      },
    },
    option,
  );
};

const getGridOptions = (option?: any) => {
  const result = _.merge(
    {
      showOverflow: true,
      border: true,
      height: 1000,
      pagerConfig: {
        enabled: true,
        total: 0,
        pageSize: 10,
        currentPage: 1,
      },
      columns: [],
      data: [],
    },
    option,
  );
  result.columns.push({
    field: 'action',
    title: '操作',
    slots: { default: 'action' },
  });
  return result;
};

export const useListTable = (params) => {
  const fcOptions = getFcOptions({ rule: computeFcOptions(params.columns) });
  const gridOptions = getGridOptions({ columns: params.columns });

  const register = async (arg) => {
    arg.syncFcOptions(fcOptions);
    arg.syncGridOptions(gridOptions);
    arg.syncTableData(params.api);
  };
  return {
    register,
  };
};
